package com.oa.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.oa.back.ResponseBack;
import com.oa.common.base.Page;
import com.oa.common.base.Pageable;
import com.oa.common.dto.RoleDto;
import com.oa.common.enums.CodeStatus;
import com.oa.dao.BaseDao;
import com.oa.dao.RoleDao;
import com.oa.entities.Role;
import com.oa.service.RoleService;
import com.oa.service.UndeleteServiceImpl;
import com.oa.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

@Service
public class RoleServiceImpl extends UndeleteServiceImpl<Long,Role> implements RoleService {

    private static final Logger logger= LoggerFactory.getLogger(RoleServiceImpl.class);

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private UserService userService;

    @Override
    protected BaseDao<Long, Role> getSelfDao() {
        return roleDao;
    }

    @Override
    public void setClass(Class<Role> clazz) {

    }

    @Override
    public Page<Role> findPage(Pageable pageable, String key) {
        return roleDao.findPage(pageable,key);
    }

    @Override
    public Boolean validate(String name,Long id) {
        return roleDao.validate(name,id);
    }

    @Override
    @Transactional
    public ResponseBack<String> submit(String params) {
        if (StringUtils.isEmpty(params)){
            return ResponseBack.fial(CodeStatus.PARAMS_FAIL.getResponseStatus());
        }
        try{
            RoleDto dto= JSONObject.parseObject(params,RoleDto.class);
            if (dto==null){
                return ResponseBack.fial(CodeStatus.PASSWORD_FAIL.getResponseStatus());
            }
            if (StringUtils.isEmpty(dto.getName())){
                return ResponseBack.fial(CodeStatus.ROLE_NAME_NOTNULL.getResponseStatus());
            }
            if (roleDao.validate(dto.getName(),null)){
                return ResponseBack.fial(CodeStatus.ROLE_EXIST.getResponseStatus());
            }
            Role role= new Role();
            role.setName(dto.getName());
            role.setDescription(dto.getDescription());
            String[] authers= dto.getAuthorities();
            role.setCreateTime(System.currentTimeMillis());
            List<String> auts= Arrays.asList(authers);
            role.setAuthorities(auts);
            save(role);
        }catch (RuntimeException ex){
            logger.error("提交的异常:",ex);
            return ResponseBack.fial(CodeStatus.PARAMS_FAIL.getResponseStatus());
        }
        return ResponseBack.success(null);
    }

    @Override
    @Transactional
    public ResponseBack<String> update(String params) {
        if (StringUtils.isEmpty(params)){
            return ResponseBack.fial(CodeStatus.PARAMS_FAIL.getResponseStatus());
        }
        try{
            RoleDto dto= JSONObject.parseObject(params,RoleDto.class);
            if (dto==null){
                return ResponseBack.fial(CodeStatus.PARAMS_FAIL.getResponseStatus());
            }
            Role role= get(dto.getId());
            if (role==null){
                return ResponseBack.fial(CodeStatus.ROLE_UNDERFUND.getResponseStatus());
            }
            if (StringUtils.isEmpty(dto.getName())){
                return ResponseBack.fial(CodeStatus.ROLE_NAME_NOTNULL.getResponseStatus());
            }
            if (roleDao.validate(dto.getName(),role.getId())){
                return ResponseBack.fial(CodeStatus.ROLE_EXIST.getResponseStatus());
            }
            role.setName(dto.getName());
            role.setDescription(dto.getDescription());
            String[] authers= dto.getAuthorities();
            List<String> auths= Arrays.asList(authers);
            role.setAuthorities(auths);
            update(role);
        }catch (RuntimeException ex){
            return ResponseBack.fial(CodeStatus.PARAMS_FAIL.getResponseStatus());
        }
        return ResponseBack.success(null);
    }

    @Override
    @Transactional
    public ResponseBack<String> disableRole(Long id) {
        Role role= get(id);
        if (role==null){
            return ResponseBack.fial(CodeStatus.ROLE_UNDERFUND.getResponseStatus());
        }
        if (userService.validateByDepartment(id)){
            return ResponseBack.fial(CodeStatus.ROLE_NO_DELETE.getResponseStatus());
        }
        disable(role);
        return ResponseBack.success(null);
    }
}
